<?php
namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Lyb extends Base
{
    protected $table = 'lyb_event';//留言板

    protected $activityCon = 'activity';
    protected $customLybConfigTable = 'custom_lyb_config';//自定义活动-留言板-配置

    //自定义活动活动留言板配置
    protected $customLybConfigKey = 'CUSTOM:LYBCONFIG:';//string CUSTOM:LYBCONFIG:{customId} => {string}

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //新增留言板
    public function addLybEvent($data)
    {
        $id = DB::table($this->table)->insertGetId($data);

        return $id;
    }

    //更新留言板机审状态
    public function updateLybEventAStatus($id, $showContent, $aResult)
    {
        $data = DB::table($this->table)
            ->where('id', $id)
            ->update(['a_status'=>1, 'show_content'=>$showContent, 'a_result'=>$aResult]);

        return $data;
    }

    //获取留言板数据
    public function getLybEvent($id)
    {
        $data = DB::table($this->table)
            ->where('id', $id)
            ->first();
        $data = self::objToArray($data);

        return $data;
    }

    //更新留言板人工审状态
    public function updateLybEventBStatus($id, $bStatus, $remind)
    {
        $data = DB::table($this->table)
            ->where('id', $id)
            ->update(['b_status'=>$bStatus, 'remind'=>$remind]);

        return $data;
    }

    /**
     * 获取自定义活动留言板配置
     */
    public function getCustomLybConfig($customId){
        $key = $this->customLybConfigKey.$customId;
        $info = Redis::get($key);
        if(empty($info)){
            $info = DB::connection($this->activityCon)->table($this->customLybConfigTable)->where('custom_id', $customId)->where('is_delete', 0)->first();
            $info = self::objToArray($info);
            if(!empty($info)) {
                $info['content'] = explode('｜', $info['content']);
                Redis::setex($key, 86400, json_encode($info));
            }
        }else{
            $info = json_decode($info, true);
        }
        if(!empty($info)) {
            $info['lyb_url'] = build_image($info['lyb_url']);
        }
        return $info;
    }

}
